//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");

var circleUrl = require("url/circle");
var http = require("helper/http");
var openHelper = require("helper/open");
var userUtil = require("helper/user");
var hashdata = mm("do_HashData");
var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);

var rootview = ui("$");
var g4template = 0,g6template = 1,g9template = 2;
var noData = [{"template":3,"labelContent":"您还没有加入任何群聊"}];

/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

ui(addheader+".labelTitle").text = "我的群聊";

/****************处理页面-头部-end*****************/

var httpUrl = circleUrl.url.base.myGroupChats;
// 全部刷新数据
function binddata() {
	http.post(httpUrl, {}, function(data) {
		listview.rebound();
		listdata.removeAll();// 清除数据
		var circleList = data.circleList;
		if (circleList) {
			if(circleList.length>0){
				listdata.addData(circleList);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}

// 下拉列表
listview.on("pull", function(data) {
	if (data.state == 2) {
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

// 上拉列表
listview.on("push", function(data) {
	if (data.state == 2) {
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

page.on("loaded", function(){
	binddata();
});

page.on("back", function() {
	app.closePage();
});

page.on("result", function(data){
	if (data.dismissGroup || data.quitGroup) {
		binddata();
    }
});

app.on("chat_name_update", function(data, e) {
	binddata();
})